<?

/**
 * DevSuite
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://suite.rapidcodefactory.com/license/new-bsd
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@rapidcodefactory.com so we can send you a copy immediately.
 *
 * @category   RFC_Model
 * @package    RCF_DModel
 * @copyright  Copyright (c) 2010-2011 Rapid Code Factory Ltd. POLAND (http://www.rapidcodefactory.com)
 * @license    http://suite.rapidcodefactory.com/license/new-bsd     New BSD License
 * @version    
*/



/**
 * Class for DB_Object Model
 *
 * @category   RFC_Model
 * @package    RFC_DModel
 * @copyright  Copyright (c) 2010-2011 Rapid Code Factory Ltd. POLAND (http://www.rapidcodefactory.com)
 * @license    http://suite.rapidcodefactory.com/license/new-bsd     New BSD License
*/
class Apps extends DomainModel {
	protected $id;	//primaryKey=yes, dataType=int, maxLength=, allowNulls=no
	protected $languagesId;	//primaryKey=no, dataType=smallint, maxLength=, allowNulls=no
	protected $statusesId;	//primaryKey=no, dataType=smallint, maxLength=, allowNulls=no
	protected $name;	//primaryKey=no, dataType=varchar, maxLength=50, allowNulls=no
	protected $configXml;	//primaryKey=no, dataType=varchar, maxLength=1500, allowNulls=no
	protected $addDate;	//primaryKey=no, dataType=timestamp, maxLength=, allowNulls=no
	protected $url;	//primaryKey=no, dataType=varchar, maxLength=255, allowNulls=yes
}




/**
 * Class for DB_Object ModelMapper
 *
 * @category   RFC_Model
 * @package    RFC_DModel
 * @copyright  Copyright (c) 2010-2011 Rapid Code Factory Ltd. POLAND (http://www.rapidcodefactory.com)
 * @license    http://suite.rapidcodefactory.com/license/new-bsd     New BSD License
*/
class AppsMapper extends ModelMapper {
	function __construct(){
		parent::__construct();
		$this->tableName = 'apps';
		$this->selectViewName = 'v_apps';
		$this->selectFields = '
				id,
				languages_id,
				statuses_id,
				name,
				config_xml,
				add_date,
				url
		';

		$this->findQuery = self::$DB->prepare("
			SELECT ".$this->selectFields."
			FROM ".$this->selectViewName."
			WHERE id = ?
		");
		$this->insertQuery = self::$DB->prepare("
			CALL p_add_apps (@p_id,
				?,
				?,
				?,
				?,
				?,
				?
		)");


		$this->updateQuery = self::$DB->prepare("
			CALL p_upd_apps (
				?,
				?,
				?,
				?,
				?,
				?,
				?

		)");

		$this->deleteQuery = self::$DB->prepare("
			CALL p_del_apps (?)
		");

		$this->cacheTime = null;
	}

	protected function doLoad($array){
		$acuire = str_replace('Mapper', '', get_class($this));
		$obj = new $acuire($array['id']);
		$obj->setId($array['id']);
		$obj->setLanguagesId($array['languages_id']);
		$obj->setStatusesId($array['statuses_id']);
		$obj->setName($array['name']);
		$obj->setConfigXml($array['config_xml']);
		$obj->setAddDate($array['add_date']);
		$obj->setUrl($array['url']);

		return $obj;
	}

	function insert(DomainModel $obj){

		$values = array(
			$obj->getLanguagesId(),
			$obj->getStatusesId(),
			$obj->getName(),
			$obj->getConfigXml(),
			$obj->getAddDate(),
			$obj->getUrl()
		);

		$this->doStatement($this->insertQuery, $values);
		$obj->setId(self::$DB->getOne("SELECT @p_id"));
	}

	function update(DomainModel $obj){
		$values = array(
			$obj->getId(),
			$obj->getLanguagesId(),
			$obj->getStatusesId(),
			$obj->getName(),
			$obj->getConfigXml(),
			$obj->getAddDate(),
			$obj->getUrl()
		);

		$this->doStatement($this->updateQuery, $values);
	}

	function delete(DomainModel $obj){
		$this->doStatement($this->deleteQuery, array($obj->getId()));
	}

	function newId(){

		$row = self::$DB->getRow('SHOW TABLE STATUS LIKE apps');
		return $row['Auto_increment'];

	}
}


/**
 * Class for DB_Object Model
 *
 * @category   RFC_ModelCollection
 * @package    RFC_DModel
 * @copyright  Copyright (c) 2010-2011 Rapid Code Factory Ltd. POLAND (http://www.rapidcodefactory.com)
 * @license    http://suite.rapidcodefactory.com/license/new-bsd     New BSD License
*/
class AppsCollection extends ModelCollection {
	function add(Apps $model){
		$this->doAdd($model);
	}
}


/**
 * Class for DB_Object Model
 *
 * @category   RFC_ModelCollectionExtended
 * @package    RFC_DModel
 * @copyright  Copyright (c) 2010-2011 Rapid Code Factory Ltd. POLAND (http://www.rapidcodefactory.com)
 * @license    http://suite.rapidcodefactory.com/license/new-bsd     New BSD License
*/
class AppsCollectionExtended extends AppsCollection {
	private $stmt;
	private $valueArray;
	private $mapper;
	private $run = false;

	function __construct(ModelMapper $mapper, $stmtHandle, $valueArray){
		parent::__construct();
		$this->stmt = $stmtHandle;
		$this->valueArray = $valueArray;
		$this->mapper = $mapper;
	}

	function notifyAccess(){
		if (!$this->run){
			$result = $this->mapper->doStatement($this->stmt, $this->valueArray,$this->mapper->getCacheTime());
			$this->init_db($result, $this->mapper);
			$this->run = true;
		}
	}
}

?>
